package com.littlebee.storychild.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;

import com.flyco.tablayout.SlidingTabLayout;
import com.littlebee.storychild.R;
import com.littlebee.storychild.common.utils.LogUtil;
import com.littlebee.storychild.common.utils.Utils;
import com.littlebee.storychild.ui.entry.User;
import com.zss.library.utils.SharedPrefUtils;

import java.util.ArrayList;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //注册极光im
        String chuanhao = "13916014463_bb";
        String password = "123456";
//        bingMessage(chuanhao, password);
        loginJG( chuanhao, password);
    }

    private void initView() {

        TestFragment mfragment1 = new TestFragment();
        Bundle data = new Bundle();
        data.putInt("currentPage", 1);
        mfragment1.setArguments(data);//通过Bundle向Activity中传递值

        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        fragmentList.add(mfragment1);

        String[] titles = new String[] { "故事"};
        //ViewPager的adapter
        FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(getSupportFragmentManager(), fragmentList, titles);
        final ViewPager pager = (ViewPager)findViewById(R.id.activity_content);
        pager.setAdapter(adapter);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //实例化TabPageIndicator然后设置ViewPager与之关联
        /**自定义部分属性*/
        SlidingTabLayout tabLayout = (SlidingTabLayout) findViewById(R.id.activity_tabbar);
        tabLayout.setViewPager(pager);
    }

    /**注册极光推送*/
    private void bingMessage(String userName,String password) {
        JMessageClient.register(userName, password, new BasicCallback() {
            @Override
            public void gotResult(int responseCode, String registerDesc) {
                if (responseCode == 0) {
                    Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    Log.i("RegisterActivity", "JMessageClient.register " + ", responseCode = " + responseCode + " ; registerDesc = " + registerDesc);
                }
            }
        });
    }

    /**
     * 登录极光IM
     */
    private void loginJG(final String userName, String password) {
        JMessageClient.login(userName, password, new BasicCallback() {
            @Override
            public void gotResult(int responseCode, String LoginDesc) {
                LogUtil.d("login sucess..");
                if (responseCode == 0) {
                    User user = new User();
                    user.setMobile(userName);
                    SharedPrefUtils prefUtils = Utils.getSharedPrefCommonFile();
                    prefUtils.put("user", user);
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }


        });
    }
}